package ntut.andengine.game;

import java.util.ArrayList;
import java.util.List;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

public class GameLevelChoose extends GameScene {

	TextureRegion mLevelsTextureRegion;
	List<TextureRegion> textureRegionList;
	TextureRegion mBackTextureRegion;
	List<Sprite> lvList;
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
		
		BitmapTexture bitmapTexture = new BitmapTexture(1024, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		textureRegionList = new ArrayList<TextureRegion>(20);
		for(int i=0; i<10; i++){		
			textureRegionList.add(BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "lv"+ String.valueOf(i+1)+ ".png", i*60, 0));
		}
		for(int i=0; i<10; i++){		
			textureRegionList.add(BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "lv"+ String.valueOf(i+11)+ ".png", i*60, 80));
		}
		mLevelsTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "levels.png", 600, 80);
		mBackTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "back.png", 600, 0);
		mEngine.getTextureManager().loadTexture(bitmapTexture);
	}

	@Override
	public Scene onLoadScene() {
		
		final Sprite levelTitle = new Sprite(240 - this.mLevelsTextureRegion.getWidth()/2, 80 - this.mLevelsTextureRegion.getHeight()/2, this.mLevelsTextureRegion);
		levelTitle.setScale(2);
		
		final Sprite back = new Sprite(40, 740, mBackTextureRegion){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameInit.class);
				}
				return true;
			}			
		};
		
		lvList = new ArrayList<Sprite>(20);
		lvList.add(new Sprite(30, 40, textureRegionList.get(10)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel11.class);
				}
				return true;
			}
		});

		lvList.add(new Sprite(30+120, 40, textureRegionList.get(0)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel1.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+240, 40, textureRegionList.get(1)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel2.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+360, 40, textureRegionList.get(11)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel12.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30, 40+120, textureRegionList.get(14)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel15.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+120, 40+120, textureRegionList.get(12)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel13.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+240, 40+120, textureRegionList.get(3)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel4.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+360, 40+120, textureRegionList.get(13)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel14.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30, 40+240, textureRegionList.get(16)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel17.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+120, 40+240, textureRegionList.get(2)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel3.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+240, 40+240, textureRegionList.get(4)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel5.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+360, 40+240, textureRegionList.get(17)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel18.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30, 40+360, textureRegionList.get(7)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel8.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+120, 40+360, textureRegionList.get(8)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel9.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+240, 40+360, textureRegionList.get(19)){//??
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel20.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+360, 40+360, textureRegionList.get(9)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel10.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30, 40+480, textureRegionList.get(5)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel6.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+120, 40+480, textureRegionList.get(15)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel16.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+240, 40+480, textureRegionList.get(18)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel19.class);
				}
				return true;
			}
		});
		
		lvList.add(new Sprite(30+360, 40+480, textureRegionList.get(6)){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameLevelChoose.this, GameLevel7.class);
				}
				return true;
			}
		});
		
		mScene = new Scene();
		for(int i=0; i<20; i++){
			lvList.get(i).setPosition(lvList.get(i).getX(), lvList.get(i).getY()+120);
			mScene.attachChild(lvList.get(i));
			mScene.registerTouchArea(lvList.get(i));
		}
		mScene.attachChild(levelTitle);
		mScene.attachChild(back);
		mScene.registerTouchArea(back);
	
		return mScene;
	}
}
